package vn.sunnet.backviet01.game.coloa.store;

import vn.sunnet.backviet01.game.coloa.constant.Constants;
import vn.sunnet.util.security.InvalidChecksumException;

import android.content.Context;
import android.content.SharedPreferences;

public class GeneralStore {
	private SharedPreferences preferences;

	private int account;

	private int name;
	private int kindOfGeneral;

	private int level;

	private int agility;
	private int intelligent;
	private int streng;

	private float EXP;
	private String EXPString;

	private int[] ID_Skill_Used;

	private int pointUpSkill;
	private int pointUpBasic;

	private int initPointUpBasic;
	private int initPointUpSkill;

	public GeneralStore(Context context, int account, int name) {
		preferences = context.getSharedPreferences(
				"vn.sunnet.backviet.game.coloa", 0);
		this.name = name;
		this.account = account;

		onLoadStep1();

	}

	public void onLoadStep1() {

		EXPString = preferences.getString(String.valueOf(account) + "general"
				+ String.valueOf(name) + "EXP", "0");

		EXP = Float.valueOf(EXPString);

		level = preferences.getInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "level", 0);

		setBasic(name);
	}

	public void onLoadStep2() {
		pointUpBasic = preferences.getInt(String.valueOf(account) + "general"
				+ String.valueOf(name) + "point_up_basic", 0);
		initPointUpBasic = preferences.getInt(String.valueOf(account)
				+ "general" + String.valueOf(name) + "init_point_up_basic", 0);

		pointUpSkill = preferences.getInt(String.valueOf(account) + "general"
				+ String.valueOf(name) + "point_up_skill", 0);
		initPointUpSkill = preferences.getInt(String.valueOf(account)
				+ "general" + String.valueOf(name) + "init_point_up_skill", 0);

		loadIDSkill();
	}

	public void reset() throws InvalidChecksumException {
		SharedPreferences.Editor editor = preferences.edit();

		pointUpBasic = 0;

		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "point_up_basic", pointUpBasic);

		pointUpSkill = 0;

		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "point_up_skill", pointUpSkill);

		EXP = 0;

		EXPString = "0";

		editor.putString(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "EXP", "0");

		initPointUpBasic = 0;
		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "init_point_up_basic", 0);

		initPointUpSkill = 0;

		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "init_point_up_skill", 0);
		level = 0;

		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "level", 0);

		for (int i = 0; i < 3; i++) {
			ID_Skill_Used[i] = -1;
			editor.putInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "skill" + String.valueOf(i), -1);
			ID_Skill_Used[i + 3] = -1;
			editor.putInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "skill" + String.valueOf(i) + "level", 1);
		}

		resetBasic(editor);
		editor.commit();
	}

	public void resetBasic(SharedPreferences.Editor editor) {
		if (name == 0) {

			this.kindOfGeneral = Constants.STRENG;
			editor.putInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "kindOfGeneral", Constants.STRENG);
			this.streng = 25;
			editor.putInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "streng", 25);
			this.agility = 11;
			editor.putInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "agility", 11);
			this.intelligent = 13;
			editor.putInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "intelligent", 13);
		} else if (name == 1) {
			this.kindOfGeneral = Constants.INTLLIGENT;
			editor.putInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "kindOfGeneral", Constants.INTLLIGENT);
			this.streng = 18;
			editor.putInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "streng", 18);
			this.agility = 15;
			editor.putInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "agility", 15);
			this.intelligent = 29;
			editor.putInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "intelligent", 29);

		} else if (name == 2) {
			this.kindOfGeneral = Constants.INTLLIGENT;
			editor.putInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "kindOfGeneral", Constants.INTLLIGENT);
			this.streng = 20;
			editor.putInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "streng", 20);
			this.agility = 30;
			editor.putInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "agility", 30);
			this.intelligent = 22;
			editor.putInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "intelligent", 22);

		}
	}

	public int getPointUpBasic() {
		return pointUpBasic;
	}

	public int getInitPointUpBasic() {
		return initPointUpBasic;
	}

	public void setPointUpBasic(int point, boolean isUp) {
		SharedPreferences.Editor editor = preferences.edit();
		if (isUp) {
			pointUpBasic += point;
		} else
			pointUpBasic -= point;
		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "point_up_basic", pointUpBasic);
		editor.commit();
	}

	public void upPointBasic(int point) {
		SharedPreferences.Editor editor = preferences.edit();
		initPointUpBasic += point;
		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "init_point_up_basic", initPointUpBasic);
		editor.commit();
	}

	public void upPointSkill(int point) {
		SharedPreferences.Editor editor = preferences.edit();
		initPointUpSkill += point;
		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "init_point_up_skill", initPointUpSkill);
		editor.commit();
		
	}

	public void setPointUpSkill(int point, boolean isUp) {
		SharedPreferences.Editor editor = preferences.edit();
		if (isUp)
			pointUpSkill += point;
		else
			pointUpSkill -= point;

		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "point_up_skill", pointUpSkill);
		editor.commit();
	}

	public int getPointUpSkill() {
		return pointUpSkill;
	}

	public int getInitPointUpSkill() {
		return initPointUpSkill;
	}

	public void setBasic(int name) {
		if (name == 0) {
			this.kindOfGeneral = this.preferences.getInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "kindOfGeneral", Constants.STRENG);
			this.streng = this.preferences.getInt(String.valueOf(account)
					+ "general" + String.valueOf(name) + "streng", 25);
			this.agility = this.preferences.getInt(String.valueOf(account)
					+ "general" + String.valueOf(name) + "agility", 11);
			this.intelligent = this.preferences.getInt(String.valueOf(account)
					+ "general" + String.valueOf(name) + "intelligent", 13);
		} else if (name == 1) {
			this.kindOfGeneral = this.preferences.getInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "kindOfGeneral", Constants.INTLLIGENT);
			this.streng = this.preferences.getInt(String.valueOf(account)
					+ "general" + String.valueOf(name) + "streng", 18);
			this.agility = this.preferences.getInt(String.valueOf(account)
					+ "general" + String.valueOf(name) + "agility", 15);
			this.intelligent = this.preferences.getInt(String.valueOf(account)
					+ "general" + String.valueOf(name) + "intelligent", 29);

		} else if (name == 2) {
			this.kindOfGeneral = this.preferences.getInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "kindOfGeneral", Constants.INTLLIGENT);
			this.streng = this.preferences.getInt(String.valueOf(account)
					+ "general" + String.valueOf(name) + "streng", 20);
			this.agility = this.preferences.getInt(String.valueOf(account)
					+ "general" + String.valueOf(name) + "agility", 30);
			this.intelligent = this.preferences.getInt(String.valueOf(account)
					+ "general" + String.valueOf(name) + "intelligent", 22);

		} else if (name == 3) {
			this.kindOfGeneral = this.preferences.getInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "kindOfGeneral", Constants.AGILITY);
			this.streng = this.preferences.getInt(String.valueOf(account)
					+ "general" + String.valueOf(name) + "streng", 50);
			this.agility = this.preferences.getInt(String.valueOf(account)
					+ "general" + String.valueOf(name) + "agility", 55);
			this.intelligent = this.preferences.getInt(String.valueOf(account)
					+ "general" + String.valueOf(name) + "intelligent", 35);
		}

	}

	public void loadIDSkill() {
		ID_Skill_Used = new int[6];
		for (int i = 0; i < 3; i++) {
			ID_Skill_Used[i] = this.preferences.getInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "skill" + String.valueOf(i), -1);
			ID_Skill_Used[i + 3] = this.preferences.getInt(
					String.valueOf(account) + "general" + String.valueOf(name)
							+ "skill" + String.valueOf(i) + "level", 1);
		}

	}

	public float getEXP() {
		return this.EXP;
	}

	public void setEXP(float exp) {
		SharedPreferences.Editor editor = preferences.edit();
		this.EXP += exp;
		this.EXPString = String.valueOf(this.EXP);
		editor.putString(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "EXP", this.EXPString);
		editor.commit();
	}

	public void upLevel() {
		SharedPreferences.Editor editor = preferences.edit();
		level++;
		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "level", level);
		editor.commit();
	}

	public int getLevel() {
		return level > 21 ? 21 : level;
	}

	public void setSkillUsed(int index, int ID) {
		SharedPreferences.Editor editor = preferences.edit();
		ID_Skill_Used[index] = ID;
		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "skill" + String.valueOf(index), ID_Skill_Used[index]);
		ID_Skill_Used[index + 3] = 1;
		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "skill" + String.valueOf(index) + "level",
				ID_Skill_Used[index + 3]);
		editor.commit();
	}

	public void removeSkillUsed(int index) {
		SharedPreferences.Editor editor = preferences.edit();
		ID_Skill_Used[index] = -1;
		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "skill" + String.valueOf(index), ID_Skill_Used[index]);
		editor.commit();
	}

	public int getSkillUsed(int index) {
		try {
			return ID_Skill_Used[index];
		} catch (Exception e) {
		}
		return -1;
	}

	public void upLevelSkillUsed(int index) {
		SharedPreferences.Editor editor = preferences.edit();
		ID_Skill_Used[index + 3]++;

		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "skill" + String.valueOf(index) + "level",
				ID_Skill_Used[index + 3]);
		editor.commit();
	}

	public int getLevelSkillUsed(int index) {
		try {
			return ID_Skill_Used[index + 3];
		} catch (Exception e) {
			return 1;
		}
	}

	public int getName() {
		return name;
	}

	public int getKindOfGeneral() {
		return kindOfGeneral;
	}

	public int getAgility() {
		return agility;
	}

	public void upgradeAgility(int agility) {
		SharedPreferences.Editor editor = preferences.edit();
		this.agility += agility;
		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "agility", this.agility);
		editor.commit();
	}

	public int getIntelligent() {
		return intelligent;
	}

	public void upgradeIntelligent(int intelligent) {
		SharedPreferences.Editor editor = preferences.edit();
		this.intelligent += intelligent;
		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "intelligent", this.intelligent);
		editor.commit();
	}

	public int getStreng() {
		return streng;
	}

	public void upgradeStreng(int streng) {
		SharedPreferences.Editor editor = preferences.edit();
		this.streng += streng;
		editor.putInt(
				String.valueOf(account) + "general" + String.valueOf(name)
						+ "streng", this.streng);
		editor.commit();
	}
}
